package greedy;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description: 452. 用最少数量的箭引爆气球
 * User: Li_yizYa
 * Date: 2025/6/26
 * Time: 22:34
 */
public class Solution12 {
    /**
     * 按起始点排序
     * @param points
     * @return
     */
    public int findMinArrowShots1(int[][] points) {
        if (points == null || points.length == 0) {
            return -1;
        }
        Arrays.sort(points, (a, b) -> Integer.compare(a[0], b[0]));
        int ret = 1;
        for (int i = 1; i < points.length; i++) {
            if (points[i][0] > points[i - 1][1]) {
                ret++;
            } else {
                points[i][1] = Math.min(points[i - 1][1], points[i][1]);
            }
        }
        return ret;
    }

    /**
     * 按终止点排序
     * @param points
     * @return
     */
    public int findMinArrowShots(int[][] points) {
        if (points == null || points.length == 0) {
            return -1;
        }
        Arrays.sort(points, (a, b) -> {
            return a[1] - b[1];
        });
        int ret = 1;
        int min = points[0][1];
        for (int i = 1; i < points.length; i++) {
            if (min >= points[i][0] && min <= points[i][1]) {
                min = Math.min(min, points[i][1]);
            } else {
                ret++;
                min = points[i][1];
            }
        }
        return ret;
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4};
        help(arr);
        for (int num : arr) {
            System.out.println(num);
        }
    }
    private static void help(int[] arr) {
        int left = 0;
        int right = arr.length - 1;
        while (left < right) {
            int tmp = arr[left];
            arr[left] = arr[right];
            arr[right] = tmp;
            left++;
            right--;
        }
    }
}
